package io.renren.modules.sys.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.sys.dao.AppointmentDao;
import io.renren.modules.sys.entity.AppointmentEntity;
import io.renren.modules.sys.service.AppointmentService;


@Service("appointmentService")
public class AppointmentServiceImpl extends ServiceImpl<AppointmentDao, AppointmentEntity> implements AppointmentService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
    	Integer userId=null;
    	if(null!=params.get("userId")&&!StringUtils.isEmpty(params.get("userId").toString())) {
    		userId=Integer.valueOf(params.get("userId").toString());
    	}
    	Integer followDate=null;
		if(null!=params.get("followDate")&&!StringUtils.isEmpty(params.get("followDate").toString())) {
			 followDate=Integer.valueOf(params.get("followDate").toString());
		}
	    Date tomorrow=null;
	    if(null!=followDate) {
	        Date today = new Date();
	        Calendar c = Calendar.getInstance();
	        c.setTime(today);
	        c.add(Calendar.DAY_OF_MONTH, followDate);// 今天+1天
	        tomorrow = c.getTime();
	    }
	    Date date=null;
	    if(null!=params.get("date")&&!StringUtils.isEmpty(params.get("date").toString())) {
	    	date=(Date) params.get("date");
	    }
	    
        Page<AppointmentEntity> page = this.selectPage(
                new Query<AppointmentEntity>(params).getPage(),
                new EntityWrapper<AppointmentEntity>()
                .addFilterIfNeed(null!=userId,"user_id={0}", userId)
                .orderBy("id", false)
                .le(null==followDate?false:true, "date", tomorrow)
                .le(null!=date,"date", date)
        );

        return new PageUtils(page);
    }

}
